<!DOCTYPE html>
<html lang="zh-cn">
    <head><meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'><meta name='description' content='记录学习MyBatis时的一些心得体会'><title>MyBatis学习笔记</title>

<link rel='canonical' href='https://luobo.cf/p/mybatis/'>

<link rel="stylesheet" href="/scss/style.min.css"><meta property='og:title' content='MyBatis学习笔记'>
<meta property='og:description' content='记录学习MyBatis时的一些心得体会'>
<meta property='og:url' content='https://luobo.cf/p/mybatis/'>
<meta property='og:site_name' content='luoboQAQ'>
<meta property='og:type' content='article'><meta property='article:section' content='Post' /><meta property='article:tag' content='Java' /><meta property='article:tag' content='笔记' /><meta property='article:tag' content='MyBatis' /><meta property='article:published_time' content='2021-08-11T11:11:10&#43;08:00'/><meta property='article:modified_time' content='2021-08-11T15:20:00&#43;08:00'/><meta property='og:image' content='https://luobo.cf/p/mybatis/86164137.jpg' />
<meta name="twitter:title" content="MyBatis学习笔记">
<meta name="twitter:description" content="记录学习MyBatis时的一些心得体会"><meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:image" content='https://luobo.cf/p/mybatis/86164137.jpg' />
    </head>
    <body class="
    article-page has-toc
">
    <script>
        (function() {
            const colorSchemeKey = 'StackColorScheme';
            if(!localStorage.getItem(colorSchemeKey)){
                localStorage.setItem(colorSchemeKey, "auto");
            }
        })();
    </script><script>
    (function() {
        const colorSchemeKey = 'StackColorScheme';
        const colorSchemeItem = localStorage.getItem(colorSchemeKey);
        const supportDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches === true;

        if (colorSchemeItem == 'dark' || colorSchemeItem === 'auto' && supportDarkMode) {
            

            document.documentElement.dataset.scheme = 'dark';
        } else {
            document.documentElement.dataset.scheme = 'light';
        }
    })();
</script>
<div class="container main-container flex 
    
        extended
    
">
    
        <div id="article-toolbar">
            <a href="https://luobo.cf" class="back-home">
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-chevron-left" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <polyline points="15 6 9 12 15 18" />
</svg>



                <span>返回</span>
            </a>
        </div>
    
<main class="main full-width">
    <article class="has-image main-article">
    <header class="article-header">
        <div class="article-image">
            <a href="/p/mybatis/">
                <img src="/p/mybatis/86164137_hudf396ad5f75caaaed9aa8bcb008fce93_362314_800x0_resize_q75_box.jpg"
                        srcset="/p/mybatis/86164137_hudf396ad5f75caaaed9aa8bcb008fce93_362314_800x0_resize_q75_box.jpg 800w, /p/mybatis/86164137_hudf396ad5f75caaaed9aa8bcb008fce93_362314_1600x0_resize_q75_box.jpg 1600w"
                        width="800" 
                        height="566" 
                        loading="lazy"
                        alt="Featured image of post MyBatis学习笔记" />
                
            </a>
        </div>
    

    <div class="article-details">
    

    <h2 class="article-title">
        <a href="/p/mybatis/">MyBatis学习笔记</a>
    </h2>

    
    <h3 class="article-subtitle">
        记录学习MyBatis时的一些心得体会
    </h3>
    

    
    <footer class="article-time">
        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-calendar-time" width="56" height="56" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <path d="M11.795 21h-6.795a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v4" />
  <circle cx="18" cy="18" r="4" />
  <path d="M15 3v4" />
  <path d="M7 3v4" />
  <path d="M3 11h16" />
  <path d="M18 16.496v1.504l1 1" />
</svg>
                <time class="article-time--published">Aug 11, 2021</time>
            </div>
        

        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clock" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <polyline points="12 7 12 12 15 15" />
</svg>



                <time class="article-time--reading">
                    阅读时长: 8 分钟
                </time>
            </div>
        
    </footer>
    
</div>
</header>

    <section class="article-content">
    <p>花了四天的时间，终于把MyBatis的视频教程全部看完了，为了以后不要那么快的遗忘掉，还是来写一下笔记比较好。</p>
<p>下面的内容都是基于<a class="link" href="https://space.bilibili.com/95256449"  target="_blank" rel="noopener"
    >遇见狂神说</a>的<a class="link" href="https://www.bilibili.com/video/BV1NE411Q7Nx"  target="_blank" rel="noopener"
    >MyBatis视频教程</a>而来，在此感谢能提供这么优秀的教程。</p>
<h2 id="简介">简介</h2>
<ul>
<li>MyBatis的作用用我自己的话说，就是不用自己手动搞JDBC了，省事，行！</li>
<li>MyBatis作用于持久层——DAO层 【DAO (Data Access Object)  数据访问对象】，通俗点说就是用来操作数据库的层</li>
<li>选择MyBatis的最大原因还是用的人多，为后面的Spring打基础_(:з)∠)_</li>
</ul>
<h2 id="第一个mybatis程序">第一个MyBatis程序</h2>
<p><strong>思路流程：搭建环境&ndash;&gt;导入MyBatis&mdash;&gt;编写代码&mdash;&gt;测试</strong></p>
<h3 id="具体代码">具体代码</h3>
<ol>
<li>
<p>创建Maven项目</p>
</li>
<li>
<p>引入依赖包</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!-- 导入依赖 --&gt;</span>
<span class="nt">&lt;dependencies&gt;</span>
    <span class="c">&lt;!-- mysql驱动 --&gt;</span>
    <span class="nt">&lt;dependency&gt;</span>
        <span class="nt">&lt;groupId&gt;</span>mysql<span class="nt">&lt;/groupId&gt;</span>
        <span class="nt">&lt;artifactId&gt;</span>mysql-connector-java<span class="nt">&lt;/artifactId&gt;</span>
        <span class="nt">&lt;version&gt;</span>8.0.25<span class="nt">&lt;/version&gt;</span>
        <span class="nt">&lt;scope&gt;</span>runtime<span class="nt">&lt;/scope&gt;</span>
    <span class="nt">&lt;/dependency&gt;</span>
    <span class="c">&lt;!-- mybatis --&gt;</span>
    <span class="nt">&lt;dependency&gt;</span>
        <span class="nt">&lt;groupId&gt;</span>org.mybatis<span class="nt">&lt;/groupId&gt;</span>
        <span class="nt">&lt;artifactId&gt;</span>mybatis<span class="nt">&lt;/artifactId&gt;</span>
        <span class="nt">&lt;version&gt;</span>3.5.7<span class="nt">&lt;/version&gt;</span>
    <span class="nt">&lt;/dependency&gt;</span>
    <span class="c">&lt;!-- junit --&gt;</span>
    <span class="nt">&lt;dependency&gt;</span>
        <span class="nt">&lt;groupId&gt;</span>junit<span class="nt">&lt;/groupId&gt;</span>
        <span class="nt">&lt;artifactId&gt;</span>junit<span class="nt">&lt;/artifactId&gt;</span>
        <span class="nt">&lt;version&gt;</span>4.12<span class="nt">&lt;/version&gt;</span>
        <span class="nt">&lt;scope&gt;</span>test<span class="nt">&lt;/scope&gt;</span>
    <span class="nt">&lt;/dependency&gt;</span>
</code></pre></div></li>
<li>
<p>在<code>resources</code>目录下创建<code>mybatis-config.xml</code>，作为MyBatis的配置文件</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="cp">&lt;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34; ?&gt;</span>
<span class="cp">&lt;!DOCTYPE configuration
</span><span class="cp">        PUBLIC &#34;-//mybatis.org//DTD Config 3.0//EN&#34;
</span><span class="cp">        &#34;http://mybatis.org/dtd/mybatis-3-config.dtd&#34;&gt;</span>

<span class="c">&lt;!--核心配置文件--&gt;</span>
<span class="nt">&lt;configuration&gt;</span>

    <span class="nt">&lt;environments</span> <span class="na">default=</span><span class="s">&#34;development&#34;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;environment</span> <span class="na">id=</span><span class="s">&#34;development&#34;</span><span class="nt">&gt;</span>
            <span class="nt">&lt;transactionManager</span> <span class="na">type=</span><span class="s">&#34;JDBC&#34;</span><span class="nt">/&gt;</span>
            <span class="nt">&lt;dataSource</span> <span class="na">type=</span><span class="s">&#34;POOLED&#34;</span><span class="nt">&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;driver&#34;</span> <span class="na">value=</span><span class="s">&#34;com.mysql.cj.jdbc.Driver&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;url&#34;</span> <span class="na">value=</span><span class="s">&#34;jdbc:mysql://localhost:3306/mybatis&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;username&#34;</span> <span class="na">value=</span><span class="s">&#34;root&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;password&#34;</span> <span class="na">value=</span><span class="s">&#34;root&#34;</span><span class="nt">/&gt;</span>
            <span class="nt">&lt;/dataSource&gt;</span>
        <span class="nt">&lt;/environment&gt;</span>
    <span class="nt">&lt;/environments&gt;</span>
<span class="c">&lt;!--每一个mapper都需要注册--&gt;</span>
    <span class="nt">&lt;mappers&gt;</span>
        <span class="nt">&lt;mapper</span> <span class="na">resource=</span><span class="s">&#34;UserMapper.xml&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/mappers&gt;</span>

<span class="nt">&lt;/configuration&gt;</span>
</code></pre></div></li>
<li>
<p>编写MyBatis工具类，在<code>utils</code>的包下面创建<code>MybatisUtils</code>类，填入以下代码</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.ibatis.io.Resources</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.ibatis.session.SqlSession</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.ibatis.session.SqlSessionFactory</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.ibatis.session.SqlSessionFactoryBuilder</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.io.IOException</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.io.InputStream</span><span class="o">;</span>

<span class="c1">//SqlSessionFactory
</span><span class="c1"></span>
<span class="cm">/**
</span><span class="cm"> * @author luoboQAQ
</span><span class="cm"> */</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MybatisUtils</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="n">SqlSessionFactory</span> <span class="n">sqlSessionFactory</span><span class="o">;</span>

    <span class="kd">static</span> <span class="o">{</span>
        <span class="k">try</span> <span class="o">{</span>
            <span class="c1">//使用Mybatis第一步：获取SqlSessionFactory
</span><span class="c1"></span>            <span class="n">String</span> <span class="n">resource</span> <span class="o">=</span> <span class="s">&#34;mybatis-config.xml&#34;</span><span class="o">;</span>
            <span class="n">InputStream</span> <span class="n">inputStream</span> <span class="o">=</span> <span class="n">Resources</span><span class="o">.</span><span class="na">getResourceAsStream</span><span class="o">(</span><span class="n">resource</span><span class="o">);</span>
            <span class="n">sqlSessionFactory</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SqlSessionFactoryBuilder</span><span class="o">().</span><span class="na">build</span><span class="o">(</span><span class="n">inputStream</span><span class="o">);</span>
        <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
        <span class="o">}</span>
    <span class="o">}</span>

    <span class="c1">//既然有了 SqlSessionFactory，顾名思义，我们可以从中获得 SqlSession 的实例。
</span><span class="c1"></span>    <span class="c1">//SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
</span><span class="c1"></span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="n">SqlSession</span> <span class="nf">getSqlSession</span><span class="o">(){</span>
        <span class="k">return</span> <span class="n">sqlSessionFactory</span><span class="o">.</span><span class="na">openSession</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></li>
<li>
<p>在<code>pojo</code>包下创建实体类</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">User</span> <span class="o">{</span>
   <span class="kd">private</span> <span class="kt">int</span> <span class="n">id</span><span class="o">;</span>  		<span class="c1">//id
</span><span class="c1"></span>   <span class="kd">private</span> <span class="n">String</span> <span class="n">name</span><span class="o">;</span>   	<span class="c1">//姓名
</span><span class="c1"></span>   <span class="kd">private</span> <span class="n">String</span> <span class="n">pwd</span><span class="o">;</span>   	<span class="c1">//密码
</span><span class="c1"></span>
   <span class="c1">//构造,有参,无参
</span><span class="c1"></span>   <span class="c1">//set/get
</span><span class="c1"></span>   <span class="c1">//toString() 
</span><span class="c1"></span><span class="o">}</span>
</code></pre></div></li>
<li>
<p>在<code>dao</code>包下创建接口类，我们需要的功能就填入到这里面</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">top.lbqaq.pojo.User</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.List</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.Map</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">UserMapper</span> <span class="o">{</span>
    <span class="c1">//模糊查询
</span><span class="c1"></span>    <span class="n">List</span><span class="o">&lt;</span><span class="n">User</span><span class="o">&gt;</span> <span class="nf">getUserLike</span><span class="o">(</span><span class="n">String</span> <span class="n">value</span><span class="o">);</span>

    <span class="c1">//查询全部用户
</span><span class="c1"></span>    <span class="n">List</span><span class="o">&lt;</span><span class="n">User</span><span class="o">&gt;</span> <span class="nf">getUserList</span><span class="o">();</span>

    <span class="c1">//根据ID查询用户
</span><span class="c1"></span>    <span class="n">User</span> <span class="nf">getUserById</span><span class="o">(</span><span class="kt">int</span> <span class="n">id</span><span class="o">);</span>

    <span class="c1">//插入用户
</span><span class="c1"></span>    <span class="kt">int</span> <span class="nf">addUser</span><span class="o">(</span><span class="n">User</span> <span class="n">user</span><span class="o">);</span>
    <span class="kt">int</span> <span class="nf">addUser2</span><span class="o">(</span><span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span><span class="n">Object</span><span class="o">&gt;</span> <span class="n">map</span><span class="o">);</span>

    <span class="c1">//修改用户
</span><span class="c1"></span>    <span class="kt">int</span> <span class="nf">updateUser</span><span class="o">(</span><span class="n">User</span> <span class="n">user</span><span class="o">);</span>

    <span class="c1">//删除用户
</span><span class="c1"></span>    <span class="kt">int</span> <span class="nf">deleteUser</span><span class="o">(</span><span class="kt">int</span> <span class="n">id</span><span class="o">);</span>
<span class="o">}</span>
</code></pre></div></li>
<li>
<p>编写<code>Mapper.xml</code>配置文件</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="cp">&lt;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34; ?&gt;</span>
<span class="cp">&lt;!DOCTYPE mapper
</span><span class="cp">        PUBLIC &#34;-//mybatis.org//DTD Mapper 3.0//EN&#34;
</span><span class="cp">        &#34;http://mybatis.org/dtd/mybatis-3-mapper.dtd&#34;&gt;</span>

<span class="c">&lt;!--namespace绑定一个对应的Dao/Mapper接口--&gt;</span>
<span class="nt">&lt;mapper</span> <span class="na">namespace=</span><span class="s">&#34;top.lbqaq.dao.UserMapper&#34;</span><span class="nt">&gt;</span>
    <span class="c">&lt;!--id就是namespace中对应的方法名，resultType：sql语句的返回值--&gt;</span>
    <span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;getUserList&#34;</span> <span class="na">resultType=</span><span class="s">&#34;top.lbqaq.pojo.User&#34;</span><span class="nt">&gt;</span>
        select * from user
    <span class="nt">&lt;/select&gt;</span>

    <span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;getUserLike&#34;</span> <span class="na">resultType=</span><span class="s">&#34;top.lbqaq.pojo.User&#34;</span><span class="nt">&gt;</span>
        select * from user where name like #{value}
    <span class="nt">&lt;/select&gt;</span>

    <span class="c">&lt;!--parameterType：传入参数类型--&gt;</span>
    <span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;getUserById&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;int&#34;</span> <span class="na">resultType=</span><span class="s">&#34;top.lbqaq.pojo.User&#34;</span><span class="nt">&gt;</span>
        select * from user where id=#{id}
    <span class="nt">&lt;/select&gt;</span>

    <span class="nt">&lt;insert</span> <span class="na">id=</span><span class="s">&#34;addUser&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;top.lbqaq.pojo.User&#34;</span><span class="nt">&gt;</span>
        insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
    <span class="nt">&lt;/insert&gt;</span>

    <span class="c">&lt;!--传递map的key--&gt;</span>
    <span class="nt">&lt;insert</span> <span class="na">id=</span><span class="s">&#34;addUser2&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;map&#34;</span><span class="nt">&gt;</span>
        insert into user (id,name,pwd) values (#{userid},#{userName},#{passWord})
    <span class="nt">&lt;/insert&gt;</span>

    <span class="nt">&lt;update</span> <span class="na">id=</span><span class="s">&#34;updateUser&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;top.lbqaq.pojo.User&#34;</span><span class="nt">&gt;</span>
        update user set name =#{name},pwd=#{pwd} where id=#{id}
    <span class="nt">&lt;/update&gt;</span>

    <span class="nt">&lt;delete</span> <span class="na">id=</span><span class="s">&#34;deleteUser&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;int&#34;</span><span class="nt">&gt;</span>
        delete from user where id=#{id}
    <span class="nt">&lt;/delete&gt;</span>
<span class="nt">&lt;/mapper&gt;</span>
</code></pre></div></li>
<li>
<p>进行测试</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.ibatis.session.SqlSession</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.junit.Test</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">top.lbqaq.pojo.User</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">top.lbqaq.utils.MybatisUtils</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.HashMap</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.List</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">UserMapperTest</span> <span class="o">{</span>
    <span class="nd">@Test</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">test</span><span class="o">()</span> <span class="o">{</span>
        <span class="c1">//获得sqlSession对象
</span><span class="c1"></span>        <span class="k">try</span> <span class="o">(</span><span class="n">SqlSession</span> <span class="n">sqlSession</span> <span class="o">=</span> <span class="n">MybatisUtils</span><span class="o">.</span><span class="na">getSqlSession</span><span class="o">())</span> <span class="o">{</span>
            <span class="c1">//执行SQL
</span><span class="c1"></span>            <span class="n">UserMapper</span> <span class="n">mapper</span> <span class="o">=</span> <span class="n">sqlSession</span><span class="o">.</span><span class="na">getMapper</span><span class="o">(</span><span class="n">UserMapper</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
            <span class="n">List</span><span class="o">&lt;</span><span class="n">User</span><span class="o">&gt;</span> <span class="n">userList</span> <span class="o">=</span> <span class="n">mapper</span><span class="o">.</span><span class="na">getUserList</span><span class="o">();</span>
            <span class="k">for</span> <span class="o">(</span><span class="n">User</span> <span class="n">user</span> <span class="o">:</span> <span class="n">userList</span><span class="o">)</span> <span class="o">{</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">user</span><span class="o">);</span>
            <span class="o">}</span>
        <span class="o">}</span>
    <span class="o">}</span>

    <span class="nd">@Test</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">getUserLike</span><span class="o">()</span> <span class="o">{</span>
        <span class="c1">//获得sqlSession对象
</span><span class="c1"></span>        <span class="k">try</span> <span class="o">(</span><span class="n">SqlSession</span> <span class="n">sqlSession</span> <span class="o">=</span> <span class="n">MybatisUtils</span><span class="o">.</span><span class="na">getSqlSession</span><span class="o">())</span> <span class="o">{</span>
            <span class="c1">//执行SQL
</span><span class="c1"></span>            <span class="n">UserMapper</span> <span class="n">mapper</span> <span class="o">=</span> <span class="n">sqlSession</span><span class="o">.</span><span class="na">getMapper</span><span class="o">(</span><span class="n">UserMapper</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
            <span class="n">List</span><span class="o">&lt;</span><span class="n">User</span><span class="o">&gt;</span> <span class="n">userList</span> <span class="o">=</span> <span class="n">mapper</span><span class="o">.</span><span class="na">getUserLike</span><span class="o">(</span><span class="s">&#34;%李%&#34;</span><span class="o">);</span>
            <span class="k">for</span> <span class="o">(</span><span class="n">User</span> <span class="n">user</span> <span class="o">:</span> <span class="n">userList</span><span class="o">)</span> <span class="o">{</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">user</span><span class="o">);</span>
            <span class="o">}</span>
        <span class="o">}</span>
    <span class="o">}</span>

    <span class="nd">@Test</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">getUserById</span><span class="o">()</span> <span class="o">{</span>
        <span class="n">SqlSession</span> <span class="n">sqlSession</span> <span class="o">=</span> <span class="n">MybatisUtils</span><span class="o">.</span><span class="na">getSqlSession</span><span class="o">();</span>
        <span class="n">UserMapper</span> <span class="n">mapper</span> <span class="o">=</span> <span class="n">sqlSession</span><span class="o">.</span><span class="na">getMapper</span><span class="o">(</span><span class="n">UserMapper</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
        <span class="n">User</span> <span class="n">user</span> <span class="o">=</span> <span class="n">mapper</span><span class="o">.</span><span class="na">getUserById</span><span class="o">(</span><span class="n">1</span><span class="o">);</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">user</span><span class="o">);</span>
        <span class="n">sqlSession</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="nd">@Test</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">addUser</span><span class="o">(){</span>
        <span class="n">SqlSession</span> <span class="n">sqlSession</span> <span class="o">=</span> <span class="n">MybatisUtils</span><span class="o">.</span><span class="na">getSqlSession</span><span class="o">();</span>
        <span class="n">UserMapper</span> <span class="n">mapper</span> <span class="o">=</span> <span class="n">sqlSession</span><span class="o">.</span><span class="na">getMapper</span><span class="o">(</span><span class="n">UserMapper</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
        <span class="kt">int</span> <span class="n">res</span> <span class="o">=</span> <span class="n">mapper</span><span class="o">.</span><span class="na">addUser</span><span class="o">(</span><span class="k">new</span> <span class="n">User</span><span class="o">(</span><span class="n">4</span><span class="o">,</span> <span class="s">&#34;小明&#34;</span><span class="o">,</span> <span class="s">&#34;123&#34;</span><span class="o">));</span>
        <span class="k">if</span><span class="o">(</span><span class="n">res</span><span class="o">&gt;</span><span class="n">0</span><span class="o">){</span>
            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;提交成功&#34;</span><span class="o">);</span>
            <span class="c1">//提交事务
</span><span class="c1"></span>            <span class="n">sqlSession</span><span class="o">.</span><span class="na">commit</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="n">sqlSession</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="nd">@Test</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">addUser2</span><span class="o">(){</span>
        <span class="n">SqlSession</span> <span class="n">sqlSession</span> <span class="o">=</span> <span class="n">MybatisUtils</span><span class="o">.</span><span class="na">getSqlSession</span><span class="o">();</span>
        <span class="n">UserMapper</span> <span class="n">mapper</span> <span class="o">=</span> <span class="n">sqlSession</span><span class="o">.</span><span class="na">getMapper</span><span class="o">(</span><span class="n">UserMapper</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>

        <span class="n">HashMap</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span><span class="n">Object</span><span class="o">&gt;</span> <span class="n">map</span><span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;&gt;();</span>
        <span class="n">map</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">&#34;userid&#34;</span><span class="o">,</span><span class="n">5</span><span class="o">);</span>
        <span class="n">map</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">&#34;userName&#34;</span><span class="o">,</span><span class="s">&#34;小绿&#34;</span><span class="o">);</span>
        <span class="n">map</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">&#34;passWord&#34;</span><span class="o">,</span><span class="s">&#34;1234&#34;</span><span class="o">);</span>
        <span class="kt">int</span> <span class="n">res</span> <span class="o">=</span> <span class="n">mapper</span><span class="o">.</span><span class="na">addUser2</span><span class="o">(</span><span class="n">map</span><span class="o">);</span>
        <span class="k">if</span><span class="o">(</span><span class="n">res</span><span class="o">&gt;</span><span class="n">0</span><span class="o">){</span>
            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;提交成功&#34;</span><span class="o">);</span>
            <span class="c1">//提交事务
</span><span class="c1"></span>            <span class="n">sqlSession</span><span class="o">.</span><span class="na">commit</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="n">sqlSession</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="nd">@Test</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">updateUser</span><span class="o">(){</span>
        <span class="n">SqlSession</span> <span class="n">sqlSession</span> <span class="o">=</span> <span class="n">MybatisUtils</span><span class="o">.</span><span class="na">getSqlSession</span><span class="o">();</span>
        <span class="n">UserMapper</span> <span class="n">mapper</span> <span class="o">=</span> <span class="n">sqlSession</span><span class="o">.</span><span class="na">getMapper</span><span class="o">(</span><span class="n">UserMapper</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
        <span class="kt">int</span> <span class="n">res</span> <span class="o">=</span> <span class="n">mapper</span><span class="o">.</span><span class="na">updateUser</span><span class="o">(</span><span class="k">new</span> <span class="n">User</span><span class="o">(</span><span class="n">4</span><span class="o">,</span> <span class="s">&#34;小明&#34;</span><span class="o">,</span> <span class="s">&#34;123456&#34;</span><span class="o">));</span>
        <span class="k">if</span><span class="o">(</span><span class="n">res</span><span class="o">&gt;</span><span class="n">0</span><span class="o">){</span>
            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;提交成功&#34;</span><span class="o">);</span>
            <span class="c1">//提交事务
</span><span class="c1"></span>            <span class="n">sqlSession</span><span class="o">.</span><span class="na">commit</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="n">sqlSession</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="nd">@Test</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">deleteUser</span><span class="o">(){</span>
        <span class="n">SqlSession</span> <span class="n">sqlSession</span> <span class="o">=</span> <span class="n">MybatisUtils</span><span class="o">.</span><span class="na">getSqlSession</span><span class="o">();</span>
        <span class="n">UserMapper</span> <span class="n">mapper</span> <span class="o">=</span> <span class="n">sqlSession</span><span class="o">.</span><span class="na">getMapper</span><span class="o">(</span><span class="n">UserMapper</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
        <span class="kt">int</span> <span class="n">res</span> <span class="o">=</span> <span class="n">mapper</span><span class="o">.</span><span class="na">deleteUser</span><span class="o">(</span><span class="n">4</span><span class="o">);</span>
        <span class="k">if</span><span class="o">(</span><span class="n">res</span><span class="o">&gt;</span><span class="n">0</span><span class="o">){</span>
            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;提交成功&#34;</span><span class="o">);</span>
            <span class="c1">//提交事务
</span><span class="c1"></span>            <span class="n">sqlSession</span><span class="o">.</span><span class="na">commit</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="n">sqlSession</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></li>
</ol>
<h3 id="注意事项">注意事项</h3>
<ul>
<li>
<p>如果<code>Mapper.xml</code>创建在<code>java</code>目录里而不是<code>resouces</code>里，需要在Maven配置里配置过滤，否则会保错</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;resources&gt;</span>
   <span class="nt">&lt;resource&gt;</span>
       <span class="nt">&lt;directory&gt;</span>src/main/java<span class="nt">&lt;/directory&gt;</span>
       <span class="nt">&lt;includes&gt;</span>
           <span class="nt">&lt;include&gt;</span>**/*.properties<span class="nt">&lt;/include&gt;</span>
           <span class="nt">&lt;include&gt;</span>**/*.xml<span class="nt">&lt;/include&gt;</span>
       <span class="nt">&lt;/includes&gt;</span>
       <span class="nt">&lt;filtering&gt;</span>false<span class="nt">&lt;/filtering&gt;</span>
   <span class="nt">&lt;/resource&gt;</span>
   <span class="nt">&lt;resource&gt;</span>
       <span class="nt">&lt;directory&gt;</span>src/main/resources<span class="nt">&lt;/directory&gt;</span>
       <span class="nt">&lt;includes&gt;</span>
           <span class="nt">&lt;include&gt;</span>**/*.properties<span class="nt">&lt;/include&gt;</span>
           <span class="nt">&lt;include&gt;</span>**/*.xml<span class="nt">&lt;/include&gt;</span>
       <span class="nt">&lt;/includes&gt;</span>
       <span class="nt">&lt;filtering&gt;</span>false<span class="nt">&lt;/filtering&gt;</span>
   <span class="nt">&lt;/resource&gt;</span>
<span class="nt">&lt;/resources&gt;</span>
</code></pre></div></li>
<li>
<p>当需要传入参数进行查询而不是传入实体类，有两种方法</p>
<ul>
<li>
<p>第一种：直接在方法中传递参数。</p>
<p>在接口方法的参数前加<code>@Param</code>属性，sql语句编写的时候，直接取<code>@Param</code>中设置的值即可，不需要单独设置参数类型</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="n">User</span> <span class="nf">selectUserByNP</span><span class="o">(</span><span class="nd">@Param</span><span class="o">(</span><span class="s">&#34;username&#34;</span><span class="o">)</span> <span class="n">String</span> <span class="n">username</span><span class="o">,</span><span class="nd">@Param</span><span class="o">(</span><span class="s">&#34;pwd&#34;</span><span class="o">)</span> <span class="n">String</span> <span class="n">pwd</span><span class="o">);</span>

<span class="cm">/*
</span><span class="cm">   &lt;select id=&#34;selectUserByNP&#34; resultType=&#34;top.lbqaq.pojo.User&#34;&gt;
</span><span class="cm">     select * from user where name = #{username} and pwd = #{pwd}
</span><span class="cm">   &lt;/select&gt;
</span><span class="cm">*/</span>
</code></pre></div></li>
<li>
<p>第二种：通过map来传递参数。（推荐）</p>
<p>在接口创建时就直接使用map作为参数</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kt">int</span> <span class="nf">addUser2</span><span class="o">(</span><span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span><span class="n">Object</span><span class="o">&gt;</span> <span class="n">map</span><span class="o">);</span>
</code></pre></div><p>在写sql时直接设置入参为<code>map</code>即可</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="o">&lt;!--</span><span class="n">传递map的key</span><span class="o">--&gt;</span>
    <span class="o">&lt;</span><span class="n">insert</span> <span class="n">id</span><span class="o">=</span><span class="s">&#34;addUser2&#34;</span> <span class="n">parameterType</span><span class="o">=</span><span class="s">&#34;map&#34;</span><span class="o">&gt;</span>
        <span class="n">insert</span> <span class="n">into</span> <span class="nf">user</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span><span class="n">name</span><span class="o">,</span><span class="n">pwd</span><span class="o">)</span> <span class="n">values</span> <span class="o">(</span><span class="err">#</span><span class="o">{</span><span class="n">userid</span><span class="o">},</span><span class="err">#</span><span class="o">{</span><span class="n">userName</span><span class="o">},</span><span class="err">#</span><span class="o">{</span><span class="n">passWord</span><span class="o">})</span>
    <span class="o">&lt;/</span><span class="n">insert</span><span class="o">&gt;</span>
</code></pre></div></li>
</ul>
</li>
</ul>
<h2 id="配置解析">配置解析</h2>
<p><a class="link" href="https://mybatis.org/mybatis-3/zh/configuration.html"  target="_blank" rel="noopener"
    >官方文档</a></p>
<h3 id="核心配置">核心配置</h3>
<p>MyBatis官方给出的配置项有以下这么多：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml">configuration（配置）
properties（属性）
settings（设置）
typeAliases（类型别名）
typeHandlers（类型处理器）
objectFactory（对象工厂）
plugins（插件）
environments（环境配置）
environment（环境变量）
transactionManager（事务管理器）
dataSource（数据源）
databaseIdProvider（数据库厂商标识）
mappers（映射器）
<span class="c">&lt;!-- 注意元素节点的顺序！顺序不对会报错 --&gt;</span>
</code></pre></div><p>我们需要关注的就是<code>properties（属性）</code>、<code>settings（设置）</code>、<code>typeAliases（类型别名）</code>、<code>plugins（插件）</code>、<code>environments（环境配置）</code>、<code>mappers（映射器）</code>这几项配置，其余的可以忽略（基本用不到）</p>
<h3 id="properties">properties</h3>
<p>数据库这些属性都是可外部配置且可动态替换的，既可以在典型的 Java 属性文件中配置，亦可通过 properties 元素的子元素来传递。</p>
<p>说白了，就是能将数据库那些配置项从核心配置中剥离出来，降低耦合。</p>
<h4 id="用法">用法</h4>
<ol>
<li>
<p>在<code>resources</code>目录下创建<code>db.properties</code>，填入下面的内容</p>
<pre><code class="language-properties" data-lang="properties">driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=root
</code></pre></li>
<li>
<p>在配置xml中导入</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!--引用外部配置文件--&gt;</span>
<span class="nt">&lt;properties</span> <span class="na">resource=</span><span class="s">&#34;db.properties&#34;</span><span class="nt">&gt;</span>
    <span class="c">&lt;!--外部文件优先级大于内部（下面）--&gt;</span>
    <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;username&#34;</span> <span class="na">value=</span><span class="s">&#34;root&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;password&#34;</span> <span class="na">value=</span><span class="s">&#34;123&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
</code></pre></div><p>正常情况下，只需要用<code>&lt;properties resource=&quot;db.properties&quot;/&gt;</code>这短短一句就行了，上面的代码是用来测试优先级的</p>
</li>
</ol>
<h3 id="settings">settings</h3>
<p>这里存放的是MyBatis的一些设置项，具体的内容在<a class="link" href="https://mybatis.org/mybatis-3/zh/configuration.html#settings"  target="_blank" rel="noopener"
    >官方文档</a>上可以查看</p>
<p>常用的设置项有以下几个：</p>
<table>
<thead>
<tr>
<th style="text-align:center">设置名</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">有效值</th>
<th style="text-align:center">默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">cacheEnabled</td>
<td style="text-align:center">全局性地开启或关闭所有映射器配置文件中已配置的任何缓存。</td>
<td style="text-align:center">true | false</td>
<td style="text-align:center">true</td>
</tr>
<tr>
<td style="text-align:center">mapUnderscoreToCamelCase</td>
<td style="text-align:center">是否开启驼峰命名自动映射，即从经典数据库列名A_COLUMN 映射到经典 Java 属性名 aColumn。</td>
<td style="text-align:center">true | false</td>
<td style="text-align:center">False</td>
</tr>
<tr>
<td style="text-align:center">logImpl</td>
<td style="text-align:center">指定 MyBatis 所用日志的具体实现，未指定时将自动查找。</td>
<td style="text-align:center">SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING</td>
<td style="text-align:center">未设置</td>
</tr>
</tbody>
</table>
<h3 id="typealiases">typeAliases</h3>
<p>类型别名是为 Java 类型设置一个短的名字。它只和 XML 配置有关，存在的意义仅在于用来减少类完全限定名的冗余。</p>
<p>也就是在<code>resultType</code>和<code>parameterType</code>中不需要写那么长的类名了</p>
<h4 id="用法-1">用法</h4>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!--可以给实体类起别名--&gt;</span>
<span class="nt">&lt;typeAliases&gt;</span>
    <span class="c">&lt;!--手动指定--&gt;</span>
    <span class="nt">&lt;typeAlias</span> <span class="na">type=</span><span class="s">&#34;top.lbqaq.pojo.User&#34;</span> <span class="na">alias=</span><span class="s">&#34;User&#34;</span><span class="nt">/&gt;</span>
    <span class="c">&lt;!--自动扫描包下所有类，别名为类名全小写--&gt;</span>
    <span class="nt">&lt;package</span> <span class="na">name=</span><span class="s">&#34;top.lbqaq.pojo&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/typeAliases&gt;</span>
</code></pre></div><p>有两种方法，第一种就是手动指定别名，第二种就是通过<code>package</code>标签自动扫描包下所有的类</p>
<p>在第二种方法下，默认别名是类名全小写（当然首字母大写也同样可用）</p>
<p>如果在类前加上<code>@Alias</code>注解，则别名就为手动设定的内容。</p>
<h3 id="plugins">plugins</h3>
<p>这里是配置mybatis的插件的地方，常用的插件有以下几个：</p>
<ul>
<li><a class="link" href="https://pagehelper.github.io/"  target="_blank" rel="noopener"
    >PageHelper</a></li>
<li><a class="link" href="https://mp.baomidou.com/"  target="_blank" rel="noopener"
    >MyBatis-Plus</a></li>
<li>&hellip; &hellip;</li>
</ul>
<p>这里是就不展开这些插件了，以后有需要再看</p>
<h3 id="environments">environments</h3>
<p>配置MyBatis的多套运行环境，将SQL映射到多个不同的数据库上，必须指定其中一个为默认运行环境（通过default指定）。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;environments</span> <span class="na">default=</span><span class="s">&#34;development&#34;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;environment</span> <span class="na">id=</span><span class="s">&#34;development&#34;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;transactionManager</span> <span class="na">type=</span><span class="s">&#34;JDBC&#34;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;dataSource</span> <span class="na">type=</span><span class="s">&#34;POOLED&#34;</span><span class="nt">&gt;</span>
            <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;driver&#34;</span> <span class="na">value=</span><span class="s">&#34;${driver}&#34;</span><span class="nt">/&gt;</span>
            <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;url&#34;</span> <span class="na">value=</span><span class="s">&#34;${url}&#34;</span><span class="nt">/&gt;</span>
            <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;username&#34;</span> <span class="na">value=</span><span class="s">&#34;${username}&#34;</span><span class="nt">/&gt;</span>
            <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;password&#34;</span> <span class="na">value=</span><span class="s">&#34;${password}&#34;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;/dataSource&gt;</span>
    <span class="nt">&lt;/environment&gt;</span>
<span class="nt">&lt;/environments&gt;</span>
</code></pre></div><ul>
<li><code>transactionManager</code>： 事务管理器 。有两个选项：
<ul>
<li><strong>JDBC</strong>：正常的</li>
<li>MANAGED：几乎啥都不干</li>
</ul>
</li>
<li><code>dataSource</code>：如何使用标准的 JDBC 数据源接口来配置 JDBC 连接对象的资源。有三个选项：
<ul>
<li>unpooled：这个数据源的实现只是每次被请求时打开和关闭连接。</li>
<li><strong>pooled</strong>：这种数据源的实现利用“池”的概念将 JDBC 连接对象组织起来 , 这是一种使得并发 Web 应用快速响应请求的流行处理方式。</li>
<li>jndi：这个数据源的实现是为了能在如 Spring 或应用服务器这类容器中使用，容器可以集中或在外部配置数据源，然后放置一个 JNDI 上下文的引用。</li>
</ul>
</li>
</ul>
<h3 id="mappers">mappers</h3>
<p>用于告诉MyBatis我们写的xml文件到底在哪，<strong>每个xml文件都需要绑定！</strong></p>
<p>常用的有以下几种方法：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!-- 使用相对于类路径的资源引用 --&gt;</span>
<span class="nt">&lt;mappers&gt;</span>
    <span class="nt">&lt;mapper</span> <span class="na">resource=</span><span class="s">&#34;top/lbqaq/dao/UserMapper.xml&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/mappers&gt;</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!--
</span><span class="c">使用映射器接口实现类的完全限定类名
</span><span class="c">需要配置文件名称和接口名称一致，并且位于同一目录下
</span><span class="c">--&gt;</span>
<span class="nt">&lt;mappers&gt;</span>
    <span class="nt">&lt;mapper</span> <span class="na">class=</span><span class="s">&#34;top.lbqaq.dao.UserMapper&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/mappers&gt;</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!--
</span><span class="c">将包内的映射器接口实现全部注册为映射器
</span><span class="c">但是需要配置文件名称和接口名称一致，并且位于同一目录下
</span><span class="c">--&gt;</span>
<span class="nt">&lt;mappers&gt;</span>
 <span class="nt">&lt;package</span> <span class="na">name=</span><span class="s">&#34;org.mybatis.builder&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/mappers&gt;</span>
</code></pre></div><h2 id="resultmap">ResultMap</h2>
<h3 id="属性名和字段名不一致">属性名和字段名不一致</h3>
<p>在实际开发中，会遇到类的属性名和数据库的字段名不一致，这时MyBatis的自动匹配将无法产生作用，这时可以使用<code>ResultMap</code>来解决这个问题。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;mapper</span> <span class="na">namespace=</span><span class="s">&#34;top.lbqaq.dao.UserMapper&#34;</span><span class="nt">&gt;</span>
    <span class="c">&lt;!--结果集映射--&gt;</span>
    <span class="nt">&lt;resultMap</span> <span class="na">id=</span><span class="s">&#34;UserMap&#34;</span> <span class="na">type=</span><span class="s">&#34;User&#34;</span><span class="nt">&gt;</span>
        <span class="c">&lt;!--column:数据库中的字段；property：实体类的属性--&gt;</span>
<span class="c">&lt;!--        &lt;result column=&#34;id&#34; property=&#34;id&#34;/&gt;--&gt;</span>
<span class="c">&lt;!--        &lt;result column=&#34;name&#34; property=&#34;name&#34;/&gt;--&gt;</span>
        <span class="nt">&lt;result</span> <span class="na">column=</span><span class="s">&#34;pwd&#34;</span> <span class="na">property=</span><span class="s">&#34;password&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/resultMap&gt;</span>

    <span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;getUserById&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;int&#34;</span> <span class="na">resultMap=</span><span class="s">&#34;UserMap&#34;</span><span class="nt">&gt;</span>
        select * from user where id=#{id}
    <span class="nt">&lt;/select&gt;</span>
<span class="nt">&lt;/mapper&gt;</span>
</code></pre></div><h3 id="多对一">多对一</h3>
<p>在开发时常常会有这样的需求，比如多个学生都被一个老师教，下面将对该实例具体展开来了解多对一如何处理。</p>
<h4 id="环境搭建">环境搭建</h4>
<ol>
<li>
<p>添加Lombok插件</p>
</li>
<li>
<p>引入Maven依赖</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;dependencies&gt;</span>
    <span class="c">&lt;!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --&gt;</span>
    <span class="nt">&lt;dependency&gt;</span>
        <span class="nt">&lt;groupId&gt;</span>org.projectlombok<span class="nt">&lt;/groupId&gt;</span>
        <span class="nt">&lt;artifactId&gt;</span>lombok<span class="nt">&lt;/artifactId&gt;</span>
        <span class="nt">&lt;version&gt;</span>1.18.20<span class="nt">&lt;/version&gt;</span>
        <span class="nt">&lt;scope&gt;</span>provided<span class="nt">&lt;/scope&gt;</span>
    <span class="nt">&lt;/dependency&gt;</span>
<span class="nt">&lt;/dependencies&gt;</span>
</code></pre></div></li>
<li>
<p>编写实体类</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@Data</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Student</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="kt">int</span> <span class="n">id</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">name</span><span class="o">;</span>

    <span class="cm">/**
</span><span class="cm">     * 学生需要关联一个老师
</span><span class="cm">     */</span>
    <span class="kd">private</span> <span class="n">Teacher</span> <span class="n">teacher</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@Data</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Teacher</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="kt">int</span> <span class="n">id</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">name</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div></li>
</ol>
<h4 id="按查询嵌套处理">按查询嵌套处理</h4>
<p>思路：</p>
<ol>
<li>
<p>获取所有学生的信息</p>
</li>
<li>
<p>根据获取的学生信息的老师ID-&gt;获取该老师的信息</p>
</li>
<li>
<p>思考问题，这样学生的结果集中应该包含老师，该如何处理呢，数据库中我们一般使用关联查询？</p>
<ol>
<li>做一个结果集映射：StudentTeacher</li>
<li>StudentTeacher结果集的类型为 Student</li>
<li>学生中老师的属性为teacher，对应数据库中为tid。
多个 [1,&hellip;）学生关联一个老师=&gt; 一对一，一对多</li>
<li>查看官网找到：association – 一个复杂类型的关联；使用它来处理关联查询</li>
</ol>
</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;getStudent&#34;</span> <span class="na">resultMap=</span><span class="s">&#34;StudentTeacher&#34;</span><span class="nt">&gt;</span>
    select * from student
<span class="nt">&lt;/select&gt;</span>

<span class="nt">&lt;resultMap</span> <span class="na">id=</span><span class="s">&#34;StudentTeacher&#34;</span> <span class="na">type=</span><span class="s">&#34;Student&#34;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;result</span> <span class="na">column=</span><span class="s">&#34;id&#34;</span> <span class="na">property=</span><span class="s">&#34;id&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;result</span> <span class="na">column=</span><span class="s">&#34;name&#34;</span> <span class="na">property=</span><span class="s">&#34;name&#34;</span><span class="nt">/&gt;</span>
    <span class="c">&lt;!--复杂的属性需要单独处理 对象：association 集合：collection--&gt;</span>
    <span class="nt">&lt;association</span> <span class="na">column=</span><span class="s">&#34;tid&#34;</span> <span class="na">property=</span><span class="s">&#34;teacher&#34;</span> <span class="na">javaType=</span><span class="s">&#34;Teacher&#34;</span> <span class="na">select=</span><span class="s">&#34;getTeacher&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/resultMap&gt;</span>

<span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;getTeacher&#34;</span> <span class="na">resultType=</span><span class="s">&#34;Teacher&#34;</span><span class="nt">&gt;</span>
    select * from teacher where id=#{tid}
<span class="nt">&lt;/select&gt;</span>
</code></pre></div><p>注意点：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;resultMap</span> <span class="na">id=</span><span class="s">&#34;StudentTeacher&#34;</span> <span class="na">type=</span><span class="s">&#34;Student&#34;</span><span class="nt">&gt;</span>
   <span class="c">&lt;!--association关联属性 property属性名 javaType属性类型 column在多的一方的表中的列名--&gt;</span>
   <span class="nt">&lt;association</span> <span class="na">property=</span><span class="s">&#34;teacher&#34;</span>  <span class="na">column=</span><span class="s">&#34;{id=tid,name=tid}&#34;</span> <span class="na">javaType=</span><span class="s">&#34;Teacher&#34;</span> <span class="na">select=</span><span class="s">&#34;getTeacher&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/resultMap&gt;</span>
<span class="c">&lt;!--
</span><span class="c">这里传递过来的id，只有一个属性的时候，下面可以写任何值
</span><span class="c">association中column多参数配置：
</span><span class="c">   column=&#34;{key=value,key=value}&#34;
</span><span class="c">   其实就是键值对的形式，key是传给下个sql的取值名称，value是片段一中sql查询的字段名。
</span><span class="c">--&gt;</span>
<span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;getTeacher&#34;</span> <span class="na">resultType=</span><span class="s">&#34;teacher&#34;</span><span class="nt">&gt;</span>
  select * from teacher where id = #{id} and name = #{name}
<span class="nt">&lt;/select&gt;</span>
</code></pre></div><p>这个还是比较难以理解的，一般我选择用第二种方法</p>
<h4 id="按结果嵌套处理">按结果嵌套处理</h4>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!--按照结果嵌套处理--&gt;</span>
<span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;getStudent2&#34;</span> <span class="na">resultMap=</span><span class="s">&#34;StudentTeacher2&#34;</span><span class="nt">&gt;</span>
    select s.id sid,s.name sname,t.name tname
    from student s,teacher t
    where s.tid=t.id
<span class="nt">&lt;/select&gt;</span>

<span class="nt">&lt;resultMap</span> <span class="na">id=</span><span class="s">&#34;StudentTeacher2&#34;</span> <span class="na">type=</span><span class="s">&#34;Student&#34;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;result</span> <span class="na">column=</span><span class="s">&#34;sid&#34;</span> <span class="na">property=</span><span class="s">&#34;id&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;result</span> <span class="na">column=</span><span class="s">&#34;sname&#34;</span> <span class="na">property=</span><span class="s">&#34;name&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;association</span> <span class="na">property=</span><span class="s">&#34;teacher&#34;</span> <span class="na">javaType=</span><span class="s">&#34;Teacher&#34;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;result</span> <span class="na">column=</span><span class="s">&#34;tname&#34;</span> <span class="na">property=</span><span class="s">&#34;name&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/association&gt;</span>
<span class="nt">&lt;/resultMap&gt;</span>
</code></pre></div><p>这种就比较好理解了，难度主要在sql部分，好在之前数据库学的还行，这种方法还是比较适合我。</p>
<h3 id="一对多">一对多</h3>
<p>一个老师会教多个学生。</p>
<h4 id="环境搭建-1">环境搭建</h4>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@Data</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Student</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="kt">int</span> <span class="n">id</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">name</span><span class="o">;</span>
    <span class="kd">private</span> <span class="kt">int</span> <span class="n">tid</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@Data</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Teacher</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="kt">int</span> <span class="n">id</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">name</span><span class="o">;</span>

    <span class="cm">/**
</span><span class="cm">     * 一个老师有多个学生
</span><span class="cm">     */</span>
    <span class="kd">private</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Student</span><span class="o">&gt;</span> <span class="n">students</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div><h4 id="按结果嵌套查询">按结果嵌套查询</h4>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!--按结果嵌套查询--&gt;</span>
<span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;getTeacher&#34;</span> <span class="na">resultMap=</span><span class="s">&#34;TeacherStudent&#34;</span><span class="nt">&gt;</span>
    select s.id sid,s.name sname,t.name tname,t.id tid
    from student s,teacher t
    where s.tid=t.id and t.id=#{tid}
<span class="nt">&lt;/select&gt;</span>

<span class="nt">&lt;resultMap</span> <span class="na">id=</span><span class="s">&#34;TeacherStudent&#34;</span> <span class="na">type=</span><span class="s">&#34;Teacher&#34;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;result</span> <span class="na">column=</span><span class="s">&#34;tid&#34;</span> <span class="na">property=</span><span class="s">&#34;id&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;result</span> <span class="na">column=</span><span class="s">&#34;tname&#34;</span> <span class="na">property=</span><span class="s">&#34;name&#34;</span><span class="nt">/&gt;</span>
    <span class="c">&lt;!--集合中的泛型信息用ofType获取--&gt;</span>
    <span class="nt">&lt;collection</span> <span class="na">property=</span><span class="s">&#34;students&#34;</span> <span class="na">ofType=</span><span class="s">&#34;Student&#34;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;result</span> <span class="na">column=</span><span class="s">&#34;sid&#34;</span> <span class="na">property=</span><span class="s">&#34;id&#34;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;result</span> <span class="na">column=</span><span class="s">&#34;sname&#34;</span> <span class="na">property=</span><span class="s">&#34;name&#34;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;result</span> <span class="na">column=</span><span class="s">&#34;tid&#34;</span> <span class="na">property=</span><span class="s">&#34;tid&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/collection&gt;</span>
<span class="nt">&lt;/resultMap&gt;</span>
</code></pre></div><p><code>JavaType</code>和<code>ofType</code>都是用来指定对象类型的。</p>
<p><code>JavaType</code>是用来指定<code>pojo</code>中属性的类型。</p>
<p><code>ofType</code>指定的是映射到list集合属性中<code>pojo</code>的类型。</p>
<h4 id="按查询嵌套">按查询嵌套</h4>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!--按查询嵌套--&gt;</span>
<span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;getTeacher2&#34;</span> <span class="na">resultMap=</span><span class="s">&#34;TeacherStudent2&#34;</span><span class="nt">&gt;</span>
    select * from teacher where id=#{tid}
<span class="nt">&lt;/select&gt;</span>
<span class="nt">&lt;resultMap</span> <span class="na">id=</span><span class="s">&#34;TeacherStudent2&#34;</span> <span class="na">type=</span><span class="s">&#34;Teacher&#34;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;collection</span> <span class="na">property=</span><span class="s">&#34;students&#34;</span> <span class="na">javaType=</span><span class="s">&#34;ArrayList&#34;</span> <span class="na">ofType=</span><span class="s">&#34;Student&#34;</span> <span class="na">select=</span><span class="s">&#34;getStudentByTeacherId&#34;</span> <span class="na">column=</span><span class="s">&#34;id&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/resultMap&gt;</span>

<span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;getStudentByTeacherId&#34;</span> <span class="na">resultType=</span><span class="s">&#34;Student&#34;</span><span class="nt">&gt;</span>
    select * from student where tid = #{tid}
<span class="nt">&lt;/select&gt;</span>
</code></pre></div><h2 id="日志工厂">日志工厂</h2>
<p>如果一个数据库相关的操作出现了问题，我们可以根据输出的SQL语句快速排查问题。可见打印出SQL语句是十分重要的。（还记得之前项目综合实践为了搞日志忙了半天，结果还是没搞出来😭）</p>
<h3 id="标准日志">标准日志</h3>
<p>在MyBatis里自带了通过控制台打印的日志功能，如果项目并不是很复杂，用它就足够了。</p>
<p>只需要在设置里设置启用就可以了。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;settings&gt;</span>
       <span class="nt">&lt;setting</span> <span class="na">name=</span><span class="s">&#34;logImpl&#34;</span> <span class="na">value=</span><span class="s">&#34;STDOUT_LOGGING&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/settings&gt;</span>
</code></pre></div><h3 id="log4j">log4j</h3>
<p>log4j相比标准日志，那不知道高到哪里去了，具体的功能我就不写了，毕竟一百度就能出来了。</p>
<p><strong>使用步骤：</strong></p>
<ol>
<li>
<p>导入log4j的包</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;dependency&gt;</span>
   <span class="nt">&lt;groupId&gt;</span>log4j<span class="nt">&lt;/groupId&gt;</span>
   <span class="nt">&lt;artifactId&gt;</span>log4j<span class="nt">&lt;/artifactId&gt;</span>
   <span class="nt">&lt;version&gt;</span>1.2.17<span class="nt">&lt;/version&gt;</span>
<span class="nt">&lt;/dependency&gt;</span>
</code></pre></div></li>
<li>
<p>在<code>resources</code>目录下新建<code>log4j.properties</code>，并填入以下配置文件</p>
<pre><code class="language-properties" data-lang="properties">#将等级为DEBUG的日志信息输出到console和file这两个目的地，console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file

#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n

#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/log.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n

#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
</code></pre></li>
<li>
<p>在MyBatis里启用</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;settings&gt;</span>
   <span class="nt">&lt;setting</span> <span class="na">name=</span><span class="s">&#34;logImpl&#34;</span> <span class="na">value=</span><span class="s">&#34;LOG4J&#34;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/settings&gt;</span>
</code></pre></div></li>
<li>
<p>在程序中使用Log4j进行输出</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.log4j.Logger</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.junit.Test</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">UserMapperTest</span> <span class="o">{</span>
    <span class="kd">static</span> <span class="n">Logger</span> <span class="n">logger</span> <span class="o">=</span> <span class="n">Logger</span><span class="o">.</span><span class="na">getLogger</span><span class="o">(</span><span class="n">UserMapperTest</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>

    <span class="nd">@Test</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">testLog4j</span><span class="o">(){</span>
        <span class="n">logger</span><span class="o">.</span><span class="na">info</span><span class="o">(</span><span class="s">&#34;info:进入了testLog4j&#34;</span><span class="o">);</span>
        <span class="n">logger</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">&#34;debug:进入了testLog4j&#34;</span><span class="o">);</span>
        <span class="n">logger</span><span class="o">.</span><span class="na">error</span><span class="o">(</span><span class="s">&#34;error:进入了testLog4j&#34;</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></li>
</ol>
<h2 id="使用注解开发">使用注解开发</h2>
<p>MyBatis最初配置信息是基于 XML ,映射语句(SQL)也是定义在 XML 中的。</p>
<p>而到MyBatis 3提供了新的基于注解的配置。不幸的是，Java 注解的的表达力和灵活性十分有限。最强大的 MyBatis 映射并不能用注解来构建。</p>
<p>在注解开发中，主要使用这几个注解：<code>@select ()</code>、<code>@update ()</code>、<code>@Insert ()</code>、<code>@delete ()</code></p>
<p>举例：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">UserMapper</span> <span class="o">{</span>

    <span class="nd">@Select</span><span class="o">(</span><span class="s">&#34;select * from user&#34;</span><span class="o">)</span>
    <span class="n">List</span><span class="o">&lt;</span><span class="n">User</span><span class="o">&gt;</span> <span class="nf">getUser</span><span class="o">();</span>

    <span class="nd">@Select</span><span class="o">(</span><span class="s">&#34;select * from user where id=#{id}&#34;</span><span class="o">)</span>
    <span class="n">User</span> <span class="nf">getUserByID</span><span class="o">(</span><span class="nd">@Param</span><span class="o">(</span><span class="s">&#34;id&#34;</span><span class="o">)</span> <span class="kt">int</span> <span class="n">id</span><span class="o">);</span>

    <span class="nd">@Insert</span><span class="o">(</span><span class="s">&#34;insert into user(id,name,pwd) values(#{id},#{name},#{pwd})&#34;</span><span class="o">)</span>
    <span class="kt">int</span> <span class="nf">addUser</span><span class="o">(</span><span class="n">User</span> <span class="n">user</span><span class="o">);</span>
<span class="o">}</span>
</code></pre></div><p>改造<code>MybatisUtils</code>工具类的<code>getSession()</code> 方法，重载实现。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="c1">//获取SqlSession连接
</span><span class="c1"></span><span class="kd">public</span> <span class="kd">static</span> <span class="n">SqlSession</span> <span class="nf">getSession</span><span class="o">(){</span>
    <span class="k">return</span> <span class="n">getSession</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span> <span class="c1">//事务自动提交
</span><span class="c1"></span><span class="o">}</span>

<span class="kd">public</span> <span class="kd">static</span> <span class="n">SqlSession</span> <span class="nf">getSession</span><span class="o">(</span><span class="kt">boolean</span> <span class="n">flag</span><span class="o">){</span>
    <span class="k">return</span> <span class="n">sqlSessionFactory</span><span class="o">.</span><span class="na">openSession</span><span class="o">(</span><span class="n">flag</span><span class="o">);</span>
<span class="o">}</span>
</code></pre></div><p>@Param注解用于给方法参数起一个名字。以下是总结的使用原则：</p>
<ul>
<li>在方法只接受一个参数的情况下，可以不使用@Param。</li>
<li>在方法接受多个参数的情况下，建议一定要使用@Param注解给参数命名。</li>
<li>如果参数是 JavaBean ， 则不能使用@Param。</li>
<li>不使用@Param注解时，参数只能有一个，并且是JavaBean。</li>
</ul>
<h2 id="动态sql">动态SQL</h2>
<p><strong>动态SQL指的是根据不同的查询条件 , 生成不同的sql语句。</strong></p>
<p>主要使用这几个标签：<code>if</code>、<code>choose</code>、<code>where</code>、<code>set</code>、<code>foreach</code></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="cp">&lt;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34; ?&gt;</span>
<span class="cp">&lt;!DOCTYPE mapper
</span><span class="cp">        PUBLIC &#34;-//mybatis.org//DTD Config 3.0//EN&#34;
</span><span class="cp">        &#34;http://mybatis.org/dtd/mybatis-3-mapper.dtd&#34;&gt;</span>

<span class="nt">&lt;mapper</span> <span class="na">namespace=</span><span class="s">&#34;top.lbqaq.dao.BlogMapper&#34;</span><span class="nt">&gt;</span>

    <span class="nt">&lt;insert</span> <span class="na">id=</span><span class="s">&#34;addBook&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;Blog&#34;</span><span class="nt">&gt;</span>
        insert into blog(id, title, author, create_time, views)
        values (#{id}, #{title}, #{author}, #{createTime}, #{views})
    <span class="nt">&lt;/insert&gt;</span>

    <span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;queryBlogIF&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;map&#34;</span> <span class="na">resultType=</span><span class="s">&#34;Blog&#34;</span><span class="nt">&gt;</span>
        select * from blog
        <span class="nt">&lt;where&gt;</span>
            <span class="nt">&lt;if</span> <span class="na">test=</span><span class="s">&#34;title != null&#34;</span><span class="nt">&gt;</span>
                title = #{title}
            <span class="nt">&lt;/if&gt;</span>
            <span class="nt">&lt;if</span> <span class="na">test=</span><span class="s">&#34;author != null&#34;</span><span class="nt">&gt;</span>
                and author = #{author}
            <span class="nt">&lt;/if&gt;</span>
        <span class="nt">&lt;/where&gt;</span>
    <span class="nt">&lt;/select&gt;</span>

    <span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;queryBlogChoose&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;map&#34;</span> <span class="na">resultType=</span><span class="s">&#34;Blog&#34;</span><span class="nt">&gt;</span>
        select * from blog
        <span class="nt">&lt;where&gt;</span>
            <span class="nt">&lt;choose&gt;</span>
                <span class="nt">&lt;when</span> <span class="na">test=</span><span class="s">&#34;title != null&#34;</span><span class="nt">&gt;</span>
                    title = #{title}
                <span class="nt">&lt;/when&gt;</span>
                <span class="nt">&lt;when</span> <span class="na">test=</span><span class="s">&#34;author != null&#34;</span><span class="nt">&gt;</span>
                    and author = #{author}
                <span class="nt">&lt;/when&gt;</span>
                <span class="nt">&lt;otherwise&gt;</span>
                    and views = #{view}
                <span class="nt">&lt;/otherwise&gt;</span>
            <span class="nt">&lt;/choose&gt;</span>
        <span class="nt">&lt;/where&gt;</span>
    <span class="nt">&lt;/select&gt;</span>

    <span class="nt">&lt;update</span> <span class="na">id=</span><span class="s">&#34;updateBlog&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;map&#34;</span><span class="nt">&gt;</span>
        update blog
        <span class="nt">&lt;set&gt;</span>
            <span class="nt">&lt;if</span> <span class="na">test=</span><span class="s">&#34;title != null&#34;</span><span class="nt">&gt;</span>
                title = #{title},
            <span class="nt">&lt;/if&gt;</span>
            <span class="nt">&lt;if</span> <span class="na">test=</span><span class="s">&#34;author != null&#34;</span><span class="nt">&gt;</span>
                author = #{author}
            <span class="nt">&lt;/if&gt;</span>
        <span class="nt">&lt;/set&gt;</span>
        where id = #{id}
    <span class="nt">&lt;/update&gt;</span>
    
    <span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;queryBlogForeach&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;map&#34;</span> <span class="na">resultType=</span><span class="s">&#34;Blog&#34;</span><span class="nt">&gt;</span>
        select * from blog
        <span class="nt">&lt;where&gt;</span>
                <span class="c">&lt;!--collection:指定输入对象中的集合属性
</span><span class="c">        			item:每次遍历生成的对象
</span><span class="c">       	 			open:开始遍历时的拼接字符串
</span><span class="c">        			close:结束时拼接的字符串
</span><span class="c">        			separator:遍历对象之间需要拼接的字符串--&gt;</span>
            <span class="nt">&lt;foreach</span> <span class="na">collection=</span><span class="s">&#34;ids&#34;</span> <span class="na">item=</span><span class="s">&#34;id&#34;</span> <span class="na">open=</span><span class="s">&#34;(&#34;</span> <span class="na">close=</span><span class="s">&#34;)&#34;</span> <span class="na">separator=</span><span class="s">&#34;or&#34;</span><span class="nt">&gt;</span>
                id = #{id}
            <span class="nt">&lt;/foreach&gt;</span>
        <span class="nt">&lt;/where&gt;</span>
    <span class="nt">&lt;/select&gt;</span>
<span class="nt">&lt;/mapper&gt;</span>
</code></pre></div><h3 id="sql片段">SQL片段</h3>
<p>有时候可能某个 sql 语句我们用的特别多，为了增加代码的重用性，简化代码，我们需要将这些代码抽取出来，然后使用时直接调用。</p>
<p><strong>提取SQL片段：</strong></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;sql</span> <span class="na">id=</span><span class="s">&#34;if-title-author&#34;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;if</span> <span class="na">test=</span><span class="s">&#34;title != null&#34;</span><span class="nt">&gt;</span>
        title = #{title}
    <span class="nt">&lt;/if&gt;</span>
    <span class="nt">&lt;if</span> <span class="na">test=</span><span class="s">&#34;author != null&#34;</span><span class="nt">&gt;</span>
        and author = #{author}
    <span class="nt">&lt;/if&gt;</span>
<span class="nt">&lt;/sql&gt;</span>
</code></pre></div><p><strong>引用SQL片段：</strong></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&#34;queryBlogIf&#34;</span> <span class="na">parameterType=</span><span class="s">&#34;map&#34;</span> <span class="na">resultType=</span><span class="s">&#34;blog&#34;</span><span class="nt">&gt;</span>
    select * from blog
    <span class="nt">&lt;where&gt;</span>
        <span class="c">&lt;!-- 引用 sql 片段，如果refid 指定的不在本文件中，那么需要在前面加上 namespace --&gt;</span>
        <span class="nt">&lt;include</span> <span class="na">refid=</span><span class="s">&#34;if-title-author&#34;</span><span class="nt">&gt;&lt;/include&gt;</span>
        <span class="c">&lt;!-- 在这里还可以引用其他的 sql 片段 --&gt;</span>
    <span class="nt">&lt;/where&gt;</span>
<span class="nt">&lt;/select&gt;</span>
</code></pre></div><p>注意：</p>
<p>①、最好基于 单表来定义 sql 片段，提高片段的可重用性</p>
<p>②、在 sql 片段中不要包括 where</p>
<h2 id="缓存">缓存</h2>
<h3 id="简介-1">简介</h3>
<p>1、什么是缓存 [ Cache ]？</p>
<ul>
<li>存在内存中的临时数据。</li>
<li>将用户经常查询的数据放在缓存（内存）中，用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询，从缓存中查询，从而提高查询效率，解决了高并发系统的性能问题。</li>
</ul>
<p>2、为什么使用缓存？</p>
<ul>
<li>减少和数据库的交互次数，减少系统开销，提高系统效率。</li>
</ul>
<p>3、什么样的数据能使用缓存？</p>
<ul>
<li>经常查询并且不经常改变的数据。</li>
</ul>
<h3 id="mybatis缓存">MyBatis缓存</h3>
<ul>
<li>
<p>MyBatis包含一个非常强大的查询缓存特性，它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率。</p>
</li>
<li>
<p>MyBatis系统中默认定义了两级缓存：<strong>一级缓存</strong>和<strong>二级缓存</strong></p>
</li>
<li>
<ul>
<li>默认情况下，只有一级缓存开启。（SqlSession级别的缓存，也称为本地缓存）</li>
<li>二级缓存需要手动开启和配置，他是基于namespace级别的缓存。</li>
<li>为了提高扩展性，MyBatis定义了缓存接口Cache。我们可以通过实现Cache接口来自定义二级缓存</li>
</ul>
</li>
</ul>
<h3 id="一级缓存">一级缓存</h3>
<p>一级缓存也叫本地缓存：</p>
<ul>
<li>与数据库同一次会话期间查询到的数据会放在本地缓存中。</li>
<li>以后如果需要获取相同的数据，直接从缓存中拿，没必须再去查询数据库；</li>
</ul>
<h4 id="一级缓存失效的四种情况">一级缓存失效的四种情况</h4>
<ol>
<li>sqlSession不同</li>
<li>sqlSession相同，查询条件不同</li>
<li>sqlSession相同，两次查询之间执行了增删改操作！</li>
<li>sqlSession相同，手动清除一级缓存</li>
</ol>
<h3 id="二级缓存">二级缓存</h3>
<ul>
<li>
<p>二级缓存也叫全局缓存，一级缓存作用域太低了，所以诞生了二级缓存</p>
</li>
<li>
<p>基于namespace级别的缓存，一个名称空间，对应一个二级缓存；</p>
</li>
<li>
<p>工作机制</p>
</li>
<li>
<ul>
<li>一个会话查询一条数据，这个数据就会被放在当前会话的一级缓存中；</li>
<li>如果当前会话关闭了，这个会话对应的一级缓存就没了；但是我们想要的是，会话关闭了，一级缓存中的数据被保存到二级缓存中；</li>
<li>新的会话查询信息，就可以从二级缓存中获取内容；</li>
<li>不同的mapper查出的数据会放在自己对应的缓存（map）中；</li>
</ul>
</li>
</ul>
<h4 id="使用方法">使用方法</h4>
<ol>
<li>
<p>开启全局缓存 <code>mybatis-config.xml</code></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;setting</span> <span class="na">name=</span><span class="s">&#34;cacheEnabled&#34;</span> <span class="na">value=</span><span class="s">&#34;true&#34;</span><span class="nt">/&gt;</span>
</code></pre></div></li>
<li>
<p>去每个<code>mapper.xml</code>中配置使用二级缓存，这个配置非常简单。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;cache/&gt;</span>

官方示例
<span class="nt">&lt;cache</span>
  <span class="na">eviction=</span><span class="s">&#34;FIFO&#34;</span>
  <span class="na">flushInterval=</span><span class="s">&#34;60000&#34;</span>
  <span class="na">size=</span><span class="s">&#34;512&#34;</span>
  <span class="na">readOnly=</span><span class="s">&#34;true&#34;</span><span class="nt">/&gt;</span>
这个更高级的配置创建了一个 FIFO 缓存，每隔 60 秒刷新，最多可以存储结果对象或列表的 512 个引用，而且返回的对象被认为是只读的，因此对它们进行修改可能会在不同线程中的调用者产生冲突。
</code></pre></div></li>
</ol>

</section>


    <footer class="article-footer">
    
    <section class="article-tags">
        
            <a href="/tags/java/">Java</a>
        
            <a href="/tags/%E7%AC%94%E8%AE%B0/">笔记</a>
        
            <a href="/tags/mybatis/">MyBatis</a>
        
    </section>


    
    <section class="article-copyright">
        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copyright" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <path d="M14.5 9a3.5 4 0 1 0 0 6" />
</svg>



        <span>Licensed under CC BY-NC-SA 4.0</span>
    </section>
    <section class="article-time">
        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clock" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <polyline points="12 7 12 12 15 15" />
</svg>



        <span class="article-time--modified">
            最后更新于 Aug 11, 2021 15:20 &#43;0800
        </span>
    </section></footer>


    
</article>

    <aside class="related-contents--wrapper">
    
    
        <h2 class="section-title">相关文章</h2>
        <div class="related-contents">
            <div class="flex article-list--tile">
                
                    
<article class="has-image">
    <a href="/p/springmvc/">
        
        
            <div class="article-image">
                <img src="/p/springmvc/65411813.846fbc1e84d3f3f6c528fb231fcd939b_hu5438825b9b6d1014226d20d231e650c2_327102_250x150_fill_q75_box_smart1.jpg" 
                        width="250" 
                        height="150" 
                        loading="lazy" 
                        data-key="springmvc" 
                        data-hash="md5-hG&#43;8HoTT8/bFKPsjH82Tmw==">
                
            </div>
        

        <div class="article-details">
            <h2 class="article-title">SpringMVC学习笔记</h2>
        </div>
    </a>
</article>
                
                    
<article class="has-image">
    <a href="/p/spring/">
        
        
            <div class="article-image">
                <img src="/p/spring/82968220.2816b8235b9dc1da1c587b9898b84180_hud03476ee92894581471a20b3a26a7972_2325845_250x150_fill_q75_box_smart1.jpg" 
                        width="250" 
                        height="150" 
                        loading="lazy" 
                        data-key="spring" 
                        data-hash="md5-KBa4I1udwdocWHuYmLhBgA==">
                
            </div>
        

        <div class="article-details">
            <h2 class="article-title">Spring学习笔记</h2>
        </div>
    </a>
</article>
                
            </div>
        </div>
    
</aside>

     
     
        
    

    <footer class="site-footer">
    <section class="copyright">
        &copy; 
        
            2020 - 
        
        2021 luoboQAQ
    </section>
    
    <section class="powerby">
        Built with <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a> <br />
        Theme <b><a href="https://github.com/CaiJimmy/hugo-theme-stack" target="_blank" rel="noopener" data-version="3.0.0">Stack</a></b> designed by <a href="https://jimmycai.com" target="_blank" rel="noopener">Jimmy</a>
    </section>
</footer>

    
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    
    <div class="pswp__bg"></div>

    
    <div class="pswp__scroll-wrap">

        
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                
                
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"integrity="sha256-ePwmChbbvXbsO02lbM3HoHbSHTHFAeChekF1xKJdleo="crossorigin="anonymous"
                defer="true"
                >
            </script><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"integrity="sha256-UKkzOn/w1mBxRmLLGrSeyB4e1xbrp4xylgAWb3M42pU="crossorigin="anonymous"
                defer="true"
                >
            </script><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css"integrity="sha256-c0uckgykQ9v5k&#43;IqViZOZKc47Jn7KQil4/MP3ySA3F8="crossorigin="anonymous"
            ><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.css"integrity="sha256-SBLU4vv6CA6lHsZ1XyTdhyjJxCjPif/TRkjnsyGAGnE="crossorigin="anonymous"
            >

            </main>
    
        <aside class="sidebar right-sidebar sticky">
            <section class="widget archives">
                <div class="widget-icon">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-hash" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <line x1="5" y1="9" x2="19" y2="9" />
  <line x1="5" y1="15" x2="19" y2="15" />
  <line x1="11" y1="4" x2="7" y2="20" />
  <line x1="17" y1="4" x2="13" y2="20" />
</svg>



                </div>
                <h2 class="widget-title section-title">目录</h2>
                
                <div class="widget--toc">
                    <nav id="TableOfContents">
  <ol>
    <li><a href="#简介">简介</a></li>
    <li><a href="#第一个mybatis程序">第一个MyBatis程序</a>
      <ol>
        <li><a href="#具体代码">具体代码</a></li>
        <li><a href="#注意事项">注意事项</a></li>
      </ol>
    </li>
    <li><a href="#配置解析">配置解析</a>
      <ol>
        <li><a href="#核心配置">核心配置</a></li>
        <li><a href="#properties">properties</a>
          <ol>
            <li><a href="#用法">用法</a></li>
          </ol>
        </li>
        <li><a href="#settings">settings</a></li>
        <li><a href="#typealiases">typeAliases</a>
          <ol>
            <li><a href="#用法-1">用法</a></li>
          </ol>
        </li>
        <li><a href="#plugins">plugins</a></li>
        <li><a href="#environments">environments</a></li>
        <li><a href="#mappers">mappers</a></li>
      </ol>
    </li>
    <li><a href="#resultmap">ResultMap</a>
      <ol>
        <li><a href="#属性名和字段名不一致">属性名和字段名不一致</a></li>
        <li><a href="#多对一">多对一</a>
          <ol>
            <li><a href="#环境搭建">环境搭建</a></li>
            <li><a href="#按查询嵌套处理">按查询嵌套处理</a></li>
            <li><a href="#按结果嵌套处理">按结果嵌套处理</a></li>
          </ol>
        </li>
        <li><a href="#一对多">一对多</a>
          <ol>
            <li><a href="#环境搭建-1">环境搭建</a></li>
            <li><a href="#按结果嵌套查询">按结果嵌套查询</a></li>
            <li><a href="#按查询嵌套">按查询嵌套</a></li>
          </ol>
        </li>
      </ol>
    </li>
    <li><a href="#日志工厂">日志工厂</a>
      <ol>
        <li><a href="#标准日志">标准日志</a></li>
        <li><a href="#log4j">log4j</a></li>
      </ol>
    </li>
    <li><a href="#使用注解开发">使用注解开发</a></li>
    <li><a href="#动态sql">动态SQL</a>
      <ol>
        <li><a href="#sql片段">SQL片段</a></li>
      </ol>
    </li>
    <li><a href="#缓存">缓存</a>
      <ol>
        <li><a href="#简介-1">简介</a></li>
        <li><a href="#mybatis缓存">MyBatis缓存</a></li>
        <li><a href="#一级缓存">一级缓存</a>
          <ol>
            <li><a href="#一级缓存失效的四种情况">一级缓存失效的四种情况</a></li>
          </ol>
        </li>
        <li><a href="#二级缓存">二级缓存</a>
          <ol>
            <li><a href="#使用方法">使用方法</a></li>
          </ol>
        </li>
      </ol>
    </li>
  </ol>
</nav>
                </div>
            </section>
        </aside>
    

        </div>
        <script 
                src="https://cdn.jsdelivr.net/npm/node-vibrant@3.1.5/dist/vibrant.min.js"integrity="sha256-5NovOZc4iwiAWTYIFiIM7DxKUXKWvpVEuMEPLzcm5/g="crossorigin="anonymous"
                defer="false"
                >
            </script><script type="text/javascript" src="/ts/main.js" defer></script>
<script>
    (function () {
        const customFont = document.createElement('link');
        customFont.href = "https://fonts.googleapis.com/css2?family=Lato:wght@300;400;700&display=swap";

        customFont.type = "text/css";
        customFont.rel = "stylesheet";

        document.head.appendChild(customFont);
    }());
</script>

    </body>
</html>
